package com.example.excelimport.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.excelimport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Insert("<script>" +
            "INSERT INTO t_user (name, age, email, department, hire_date, create_time, update_time) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.name}, #{item.age}, #{item.email}, #{item.department}, #{item.hireDate}, NOW(), NOW())" +
            "</foreach>" +
            "</script>")
    void batchInsert(@Param("list") List<User> userList);

    @Update("TRUNCATE TABLE t_user")
    void truncateTable();

    /**
     * 按部门统计
     */
    @Select("SELECT department, COUNT(*) as count FROM t_user GROUP BY department")
    List<Map<String, Object>> countByDepartment();

    /**
     * 年龄统计
     */
    @Select("SELECT " +
            "AVG(age) as avgAge, " +
            "MIN(age) as minAge, " +
            "MAX(age) as maxAge, " +
            "COUNT(*) as total " +
            "FROM t_user")
    Map<String, Object> getAgeStatistics();
}
